package de.bloxel.ui;

import static de.bloxel.core.BloxelFactory.BloxelType.GLASS;
import static de.bloxel.core.BloxelFactory.BloxelType.GRASS;
import static de.bloxel.core.BloxelFactory.BloxelType.POND;
import static de.bloxel.core.BloxelFactory.BloxelType.WATER;

import com.jme3.input.controls.ActionListener;

import de.bloxel.core.BloxelFactory;
import de.bloxel.core.BloxelFactory.BloxelType;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.elements.render.PanelRenderer;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.tools.Color;

/**
 * Simple concept of a inventory.
 * 
 * <p>
 * <ul>
 * <li>de.bloxel.ui.BloxelApplication.initKeys() - register keybindings - the handler would be the {@link BloxelInventory}</li>
 * <li>the {@link BloxelInventory} maps the keys to the "materials"</li>
 * <li> {@link BloxelApplication#createBloxel(com.jme3.math.Vector3f)} will use the {@link BloxelFactory} to create a new bloxel for the currenlty
 * selected {@link BloxelType} - get it from the {@link BloxelInventory}</li>
 * </ul>
 * </p>
 * 
 * @author andreas
 * @author klaus
 * @since 0.1.0
 */
public class BloxelInventory implements ActionListener {

  public static final String BLOXEL_TYPE = "Bloxel_Type_";
  public static final String BLOXEL_TYPE_1 = "Bloxel_Type_1";
  public static final String BLOXEL_TYPE_2 = "Bloxel_Type_2";
  public static final String BLOXEL_TYPE_3 = "Bloxel_Type_3";
  public static final String BLOXEL_TYPE_4 = "Bloxel_Type_4";
  public static final String BLOXEL_TYPE_5 = "Bloxel_Type_5";
  public static final String BLOXEL_TYPE_6 = "Bloxel_Type_6";
  public static final String BLOXEL_TYPE_7 = "Bloxel_Type_7";
  public static final String BLOXEL_TYPE_8 = "Bloxel_Type_8";
  public static final String BLOXEL_TYPE_9 = "Bloxel_Type_9";
  public static final String BLOXEL_TYPE_0 = "Bloxel_Type_0";

  private BloxelType selectedBoxelType = BloxelType.GRASS;
  private final PanelRenderer[] panelRenderers = new PanelRenderer[10];
  private int slot = 1;

  public BloxelInventory(final Nifty theNifty) {
    for (int i = 0; i < 10; i++) {
      panelRenderers[i] = getPanelRendererForSlot(theNifty, i);
      // deselect(i);
    }
    // select(slot);
  }

  private void deselect(final int theSlot) {
    panelRenderers[theSlot].setBackgroundColor(new Color(0.2f, 0.2f, 0.2f, 0.6f));
  }

  private PanelRenderer getPanelRendererForSlot(final Nifty theNifty, final int theSlotIndex) {
    final Screen inventoryScreen = theNifty.getScreen("hud");
    final Element slot = inventoryScreen.findElementByName(String.format("slot%d", theSlotIndex));
    return slot.getRenderer(PanelRenderer.class);

    // example how to switch images

    // NiftyImage img = nifty.getRenderEngine().createImage("Interface/Images/image.png", false);
    // Element niftyElement = nifty.getCurrentScreen().findElementByName("myElement");
    // niftyElement.getRenderer(ImageRenderer.class).setImage(img);
  }

  public BloxelType getSelectedBoxelType() {
    return selectedBoxelType;
  }

  public int getSlot() {
    return slot;
  }

  @Override
  public void onAction(final String name, final boolean isPressed, final float tpf) {
    if (name.equals(BLOXEL_TYPE_1) && !isPressed) {
      setSelectedBoxelType(GLASS);
    }
    if (name.equals(BLOXEL_TYPE_2) && !isPressed) {
      setSelectedBoxelType(GRASS);
    }
    if (name.equals(BLOXEL_TYPE_3) && !isPressed) {
      setSelectedBoxelType(POND);
    }
    if (name.equals(BLOXEL_TYPE_4) && !isPressed) {
      setSelectedBoxelType(WATER);
    }
    if (!isPressed) {
      //      deselect(slot);
      slot = Integer.valueOf(name.replaceAll(BLOXEL_TYPE, ""));
      //      select(slot);
    }
  }

  private void select(final int theSlot) {
    panelRenderers[theSlot].setBackgroundColor(new Color(0.6f, 0.6f, 0.6f, 0.6f));
  }

  public void setSelectedBoxelType(final BloxelType theSelectedBoxelType) {
    selectedBoxelType = theSelectedBoxelType;
  }

}
